var express=require("express")
var fs=require("fs")
//存头像
var multer=require("multer")
//mysql
// var bodyParser=require('body-parser')
var mysql=require("mysql")
var app=express()
app.use(express.static("public"))
app.use(express.urlencoded({extended:false}))
// app.use(bodyParser.urlencoded({extended:false}))

app.listen(8888,function(){
    console.log('服务器以启动,请访问：8888端口')
})
//上传图片
//配置硬盘存储信息，包括存储位置，和存储文件名
//上传商铺图片
var myStorage=multer.diskStorage({
      //设置文件存储的目标位置
      destination(req,file,callback){
        //第一个参req就是请求对象  
        // console.log(req)
        //第二个参数file是上传文件信息，不是文件本身
        //   console.log(file)
          //第三个参数callback是设置完成的回调函数，需要手动调用确认完成
          callback(null, './public/imgs/shop')
      },
      //设置文件存储的文件名，参数和destination参数一致
      filename(req,file,callback){
        //   console.log(req,file)
          callback(null,file.originalname)
      }
})
//创建硬盘存储函数，参数是一个对象，storage表示存储配置信息对象
var save=multer({storage:myStorage})

//上传头像
var myStorage1=multer.diskStorage({
    destination(req,file,callback){
        callback(null,'./public/imgs/user')
    },
    filename(req,file,callback){
        // console.log(file)
        // console.log("快递费",req.body)
         callback(null,req.body.username+".png")
    }
})

var save1=multer({storage:myStorage1})


//定义数据库配置对象
var option={
    host:'localhost',
    port:3306,
    user:'root',
    password:'root',
    database:'can'
}

//注册接口
app.get('/register',function(req,res){
    // console.log("====",req.query)
    //连接数据库
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:"服务器错误，请重试"
            })
        }else{
            //通过注册请求的账号条件查询这个用户信息
            sqlObj.query(`select * from user where username='${req.query.username}'`,function(err,data){
                // console.log(err,data);
                if(data.length<=0){
                    
                    //err有值,说明查不到，把这个用户插入进去
                    sqlObj.query(`insert into user (username,password,who,time) values ('${req.query.username}','${req.query.password}','${req.query.radio}','${(new Date()).toLocaleDateString()}')`)
                    res.json({
                        code:1,
                        message:'注册成功'
                    })
                }else{
                    res.json({
                        code:0,
                        message:'用户名已存在，注册失败'
                    })
                }
                sqlObj.end();
            })
        }
    })

})

//登录接口
app.get('/login',function(req,res){
    // console.log(req.query)
    //连接数据库
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'服务器错误，请重试'
            })
        }else{
            sqlObj.query(`select * from user where username='${req.query.username}' and password='${req.query.password}'`,function(err,data){
                // console.log(err,data)
                if(data.length<=0){
                    res.json({
                        code:0,
                        msg:'账号或密码错误'
                    })
                }else{
                    res.json({
                        code:1,
                        data:data,
                        msg:'登录成功'
                    })
                }
                sqlObj.end();
            })

        }
    })
})

//获取用户人数、管理员人数、订单数量
app.get('/houTai',function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'连接失败'
            })
        }else{
            //获取用户数量
           var promise1=new Promise((resolve,reject)=>{
               sqlObj.query(`select count(Id) from account`,function(err1,data){
                //    console.log(data[0]['count(Id)']);
                   resolve(data[0]['count(Id)'])
               })
           });
           var promise2=new Promise((resolve,reject)=>{
               sqlObj.query(`select count(Id) from dingdan`,function(err1,data){
                //    console.log(data[0]['count(Id)'])
                   resolve(data[0]['count(Id)'])
               })

           })
           var promise3=new Promise((resolve,reject)=>{
               sqlObj.query(`select count(Id) from user`,function(err1,data){
                //    console.log(data[0]['count(Id)'])
                   resolve(data[0]['count(Id)'])
               })
           })
           let all=Promise.all([promise1,promise2,promise3]);
           all.then(data=>{
               console.log(data);
               if(data.length>0){
                        let obj = [{
                            name:'注册用户',
                            count: data[0]
                        }, {
                            name:'订单',
                            count: data[1]
                        }, {
                            name:'管理员',
                            count: data[2]
                        }]
                        res.json({
                            code:1,
                            msg:'成功',
                            obj
                        })
               }else{
                   res.json({
                       code:0,
                       msg:'失败'
                   })
               }
               sqlObj.end()
              
           })
        }
    })
})


//添加商铺
app.post('/addShop', function (req, res) {
    // console.log(req.body)
    var sqlObj = mysql.createConnection(option);
    sqlObj.connect(function (err) {
        if (err) {
            res.json({
                code: 0,
                message: '服务器错误，请重试'
            })
        } else {
            sqlObj.query(`select * from shop_details`, function (err, data) {
                if (data.length <= 0) {
                    sqlObj.query(`insert into shop_details 
                    (name,address,phone,jshao,tedian,pei_money,qi_money,start_time,shop_img,yingY_img,end_time)
                     values ('${req.body.name}', '${req.body.address}', '${req.body.phone}',
                     '${req.body.jshao}',  '${req.body.tedian}', '${req.body.pei_money}',
                     '${req.body.qisong_money}','${req.body.start_time}','${req.body.shop_img}' ,
                    '${req.body.yingY_img}','${req.body.end_time}')`)
                    res.json({
                        code:1,
                        message:'添加成功'
                    })
    
                } else {
                    sqlObj.query(`update shop_details set name='${req.body.name}',address='${req.body.address}',
                    jshao='${req.body.jshao}',phone='${req.body.phone}',shop_img='${req.body.shop_img}',
                    yingY_img='${req.body.yingY_img}',start_time='${req.body.start_time}',end_time='${req.body.end_time}',
                    qi_money='${req.body.qisong_money}',pei_money='${req.body.pei_money}',tedian='${req.body.tedian}' 
                    where id=1`)
                    res.json({
                        code: 1,
                        message: '更新成功'
                    })
                }
                sqlObj.end();
            })
        }
    })
})
//上传商铺图片
app.post('/addShop/add_img', save.single('file'), function (req, res) {
    if(req.file===undefined){
        return res.json({
            code:0,
            message:'没文件'
        })
    }
    const {size,mimetype,filename}=req.file;
    const types=['jpg','jpeg','png','gif'];
    const tmpTypes=mimetype.split('/')[1];
    
    console.log(req.file)
    file=req.file
    fs.readFile('./public/shop/file'+file.filename+"",function(err,res){
        if(err){
            // console.log(err)
        }
        // console.log(res)
    })
    res.json({
        code:0,
        message:"上传成功",
        file,
        // url
    })
  
})

//上传头像
app.post("/addUserImg",save1.single('file'),function(req,res){
    console.log("4532",req.file,req.body)
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:"服务器错误" 
            })
        }else{
            sqlObj.query(`update user set user_img='${req.file.filename}' where id='${req.body.Id}'`,function(err,data){
                // console.log(data);
                sqlObj.query(`select * from user where id='${req.body.Id}'`,function(err1,data1){
                    res.json({
                        code: 1,
                        message: "成功",
                        file: req.file,
                        data:data1
                    })
                })
                
            })
        }
    })
})
//添加食品
app.post('/addFood',function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:'服务器错误，请重试'
            })
        }else{
            sqlObj.query(`insert into food (name,food_lei,
                details,food_img,tedian,
                bao_money,price,jshao,shop_Id)
                   values ('${req.body.name}',
                   '${req.body.food_lei}','${req.body.xiangQ}',
                   '${req.body.food_img}','${req.body.tedian}',
                   '${req.body.baoMoney}','${req.body.price}','${req.body.jshao}',
                   '${req.body.shop_Id}')`)
            res.json({
                code:1,
                message:'添加成功'
            })
            sqlObj.end()
        }
    })
})
//删除商铺
//店铺列表
app.post('/shopList',function(req,res){
    console.log(req.body)
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:'服务器错误，请重试'
            })
        }else{
            sqlObj.query(`select * from shop_details`,function(errs,data){
                //  sqlObj.query(`select * from shop where id limit ${(req.body.yeNum-1)*10},10`, function (err, data) {
                    //  console.log(err,data)
                     if (errs) {
                         res.json({
                             code: 0,
                             message: '没有商铺'
                         })
                     } else {
                         // console.log(data);
                         res.json({
                             code: 1,
                             shopList: data,
                             
                         })

                     }
                     sqlObj.end();
                //  })
            })
            
        }
    })
})



//管理员列表
app.get('/admin',function(req,res){
    console.log(req.query);
    var sqlObj=mysql.createConnection(option)
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:'服务器错误，请重试'
            })
        }else{
            sqlObj.query(`select * from user`,function(err,data){
                sqlObj.query(`select * from user where id limit ${(req.query.yeNum-1)*10},10`,function(err2,data2){
                     if (data.length <= 0) {
                         res.json({
                             code: 0,
                             message: '没有用户'
                         })
                     } else {
                         res.json({
                             code: 1,
                             adminList: data2,
                             ye:data.length
                         })
                     }
                })
                
                sqlObj.end();
            })
        }
    })
})
//食品列表
app.get('/foodList',function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:'服务器错误，请重试'
            })
        }else{
            sqlObj.query(`select count(Id) from food`,function(err1,data1){
                //分页
                sqlObj.query(`select * from food where id limit ${(req.query.yeNum-1)*10},10`,function(err2,data2){
                    if(err2){
                        res.json({
                            code:0,
                            message:'没有食品'
                        })
                    }else{
                        res.json({
                            code:1,
                            foodList:data2,
                            ye:data1[0]['count(Id)']
                        })
                    }
                    sqlObj.end();
                })
            })
        }
    })
})
//食品推荐
app.post('/tjFood',function(req,res){
    var sqlObj=mysql.createConnection(option)
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:'连接失败'
            })
        }else{
            sqlObj.query(`select * from food where food_lei='${req.body.food_lei}'`,function(err1,data1){
                if(err1){
                    res.json({
                        code:1,
                        message:'查找失败'
                    })
                }else{
                    res.json({
                        code:2,
                        message:'成功',
                        data:data1
                    })
                }
                sqlObj.end();
            })
        }
    })
})
//所有食品
app.get('/Food',function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:'连接失败'
            })
        }else{
            sqlObj.query(`select * from food`,function(err1,data){
                if(err1){
                    res.json({
                        code:1,
                        message:'查询出错'
                    })
                }else{
                    res.json({
                        code:2,
                        message:'成功',
                        data,
                    })
                }
                sqlObj.end();
            })
        }
    })
})

//更新食品
app.post("/updateFood",function(req,res){
    // console.log(req.body)
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:'服务器错误'
            })
        }else{
            sqlObj.query(`update food set 
              name='${req.body.name}',food_lei='${req.body.classfy}',
              jshao='${req.body.jshao}',details='${req.body.details}',
              bao_money='${req.body.bao_money}',food_img='${req.body.img_url}'
              where id='${req.body.Id}'`,function(err1,data){
                //   console.log(err1)
                if(err1){
                      res.json({
                          code:0,
                          message:'更新失败'
                      })
                  }else{
                      res.json({
                          code:1,
                          message:'更新成功'
                      })
                  }
              })
        }
    })
})
//删除食品
app.post("/deleteFood",function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:'连接失败'
            })
        }else{
            sqlObj.query(`delete from food where id='${req.body.Id}'`,function(err1){
                if(err1){
                    res.json({
                        code:1,
                        message:'删除失败'
                    })
                }else{
                    res.json({
                        code:2,
                        message:'删除成功'
                    })
                }
            })
        }
    })
})

//搜索食物
app.get('/search',function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                message:'连接失败'
            })
        }else{
            sqlObj.query(`select * from food where name like '%${req.query.keyword}%' or jshao like '%${req.query.keyword}%'`,function(err1,data){
                // console.log(err1,data)
                if(err1){
                    res.json({
                        code:1,
                        message:'查询失败'
                    })
                }else{
                    res.json({
                        code:2,
                        message:'查询成功',
                        data,
                    })
                }
                sqlObj.end()
            })
        }
    })
})
//用户注册
app.post("/Register",function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'连接失败'
            })
        }else{
            sqlObj.query(`select * from account where username='${req.body.username}'`,function(err1,data1){
                if(data1.length<=0){
                     var date = new Date();
                    sqlObj.query(`insert into account (username,password,user_time)
                     values('${req.body.username}', '${req.body.password}', '${date.toLocaleDateString()} ${date.getHours()}:${date.getMinutes()}')
                     `)
                     res.json({
                         code:1,
                         msg:'注册成功'
                     })
                }else{
                    res.json({
                        code:0,
                        msg:'用户名已存在，请重新注册'
                    })
                }
                sqlObj.end()
            })
        }
    })
})

//用户登录
app.post("/Login",function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'连接失败'
            })
        }else{
            sqlObj.query(`select * from account where username='${req.body.username}' and password='${req.body.password}'`,function(err1,data1){
               if(data1.length<=0){
                   res.json({
                       code:0,
                       msg:'账号或密码错误'
                   })
               }else{
                   res.json({
                       code:1,
                       data:data1,
                       msg:"登录成功"
                   })
               }
               sqlObj.end()
            })
        }
    })
})
//下订单
app.get('/ding',function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'链接失败'
            })
        }else{
            console.log(req.query,req.query.Id)
            sqlObj.query(`select * from account where Id=${req.query.Id}`,function(err1,data){
                console.log(data,data.zhi,req.query.mima);
                if(data[0].zhi==req.query.mima){
                     var date=new Date();
                     sqlObj.query(`insert into dingdan (dingNum,time,food_id,money,address,phone,account_Id,ding_Id) 
                       values('${req.query.dingNum}', '${date.toLocaleDateString()} ${date.getHours()}:${date.getMinutes()}',
                       '${req.query.food_id}','${req.query.money}','${req.query.address}','${req.query.phone}','${req.query.Id}','${date.getTime()+req.query.Id}')`,function(err2,data2){
                        //    JSON.parse(req.query.food_id).forEach(item.)
                        if(err2){
                               res.json({
                                   code:0,
                                   msg:'下单失败'
                               })
                           }else{
                               res.json({
                                   code:1,
                                   msg:'支付成功',
                               })
                           }
                          
                       })
                }else{
                    res.json({
                        code:0,
                        msg:'密码错误，支付失败'
                    })
                }
                 sqlObj.end()
            })
           
        }
    })
})
//所有订单列表
app.get('/dDList',function(req,res){
    var sqlObj=mysql.createConnection(option)
    console.log(req.query)
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'连接失败'
            })
        }else{
            sqlObj.query(`select count(Id) from dingdan`,function(err1,data1){
                  sqlObj.query(`select dingdan.*,account.username,account.phone,account.user_time 
                                from dingdan,account 
                                where dingdan.account_Id=account.Id 
                                and dingdan.id order by dingdan.Id desc 
                                limit ${(req.query.yeNum-1)*10},10`, function (err2, data2) {
                    // console.log(data2)
                    new Promise((resolve,reject)=>{
                        let array=[];
                        data2.forEach((item,i,arrays)=>{
                            new Promise((resolves,rejects)=>{
                                if(item.isTrue_pei!==0){
                                    sqlObj.query(`select * from pei_people where id=${item.pei_Id}`,function(err4,data4){
                                        item.pei_people=data4[0]
                                    })
                                }
                                let content=[];
                                JSON.parse(item.food_id).forEach((items,index,arr)=>{
                                    sqlObj.query(`select * from food where id=${items}`,function(err3,data3){
                                       content = content.concat(data3)
                                       if(arr.length==index+1){
                                           resolves(content)
                                       }
                                    })
                                    
                                })
                            }).then(data=>{
                                item.arr=data;
                                array.push(item);
                                if(arrays.length==i+1){
                                    resolve(array)
                                }
                            })
                        })
                    }).then(data=>{
                        if(data.length>0){
                            res.json({
                                code:1,
                                msg:'成功',
                                dDList:data,
                                ye:data1[0]['count(Id)']
                            })
                        }else{
                             res.json({
                                 code: 0,
                                 msg: '查询失败'
                             })
                        }
                        sqlObj.end()
                    })
                    
                   
                  })
            })
           
        }
    })
})
//用户列表
app.get('/userList',function(req,res){
    var sqlObj=mysql.createConnection(option);
    console.log(req.query)
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'链接失败'
            })
        }else{
            sqlObj.query(`select count(Id) from account`,function(err1,data1){
                sqlObj.query(`select * from account where id limit ${(req.query.yeNum-1)*10},10`,function(err2,data){
                    if(data1.length<=0){
                        
                        res.json({
                            code:0,
                            msg:'无数据'
                        })
                    }else{
                        console.log(data)
                        res.json({
                            code:1,
                            msg:'成功',
                            userList:data,
                            ye:data1[0]['count(Id)'],
                        })
                    }
                    sqlObj.end()
                })
            })
        }
    })
})
//用户订单列表
app.get('/userDd',function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'链接失败'
            })
        }else{
            sqlObj.query(`select * from dingdan where account_Id='${req.query.Id}'`,function(err1,data){
                new Promise((resolve,reject)=>{
                    let array = [];
                        data.forEach((element,index,arrays) => {
                            // console.log(JSON.parse(element.food_id))

                            new Promise((resolve, reject) => {
                                if (element.isTrue_pei !== 0) {
                                    sqlObj.query(`select * from pei_people where id=${element.pei_Id}`, function (err4, data4) {
                                        element.pei_people = data4[0]
                                    })
                                }
                                let content = [];
                                
                                JSON.parse(element.food_id).forEach((item, i) => {
                                    sqlObj.query(`select * from food where Id=${item}`, function (err2, data2) {
                                        // console.log(data2[0]);
                                        content = content.concat(data2)
                                        //  console.log(content)
                                        if (JSON.parse(element.food_id).length == (i + 1)) {
                                            resolve(content)
                                        }

                                    })
                                });
                            }).then(data => {
                                element.arr = data;
                                array.push(element);
                                // console.log(arrays.length,index)
                                if(arrays.length==index+1){
                                    resolve(array)
                                }
                            })
                        });
                       
                }).then(data=>{
                    //   console.log(data)
                      if (data.length > 0) {
                          res.json({
                              code: 1,
                              msg: '成功',
                              userDd: data,
                          })
                      } else {
                          res.json({
                              code: 0,
                              msg: '失败'
                          })
                      }
                      sqlObj.end()
                })
               
               
            })
        }
    })
})
//添加配送信息
app.post("/updateDing",function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'连接失败'
            })
        }else{
            if(req.body.pei_Id){
                 sqlObj.query(`update dingdan set pei_Id=${req.body.pei_Id},isTrue_pei=${req.body.isTrue_pei} where ding_Id=${req.body.ding_Id}`, function (err1, data1) {
                     if (err1) {
                         res.json({
                             code: 0,
                             msg: '选择失败'
                         })
                     } else {
                         res.json({
                             code: 1,
                             msg: '成功'
                         })
                     }
                     sqlObj.end()
                 })
            }else{
                sqlObj.query(`update dingdan set isTrue_pei=${req.body.isTrue_pei} where Id=${req.body.ding_Id}`,function(err1,data1){
                     if (err1) {
                         res.json({
                             code: 0,
                             msg: '选择失败'
                         })
                     } else {
                         res.json({
                             code: 1,
                             msg: '成功'
                         })
                     }
                     sqlObj.end()
                })
            }
           
        }
    })
})
//修改用户手机号
app.post('/phone',function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'连接错误'
            })
        }else{
            sqlObj.query(`update account set phone='${req.body.phone}' where id=${req.body.Id}`,function(err1,data1){
                console.log(err1)
                if(err1){
                    res.json({
                        code:0,
                        msg:'更新失败'
                    })
                }else{
                    res.json({
                        code:1,
                        msg:'更新成功'
                    })
                }
                sqlObj.end()
            })
        }
    })
})
//上传用户头像
var myS2=multer.diskStorage({
    destination(req,file,callback){
        // console.log(req.body,file)
        callback(null,'./public/imgs/account')
    },
    filename(req,file,callback){
        // console.log("fei",req.body)
        callback(null, file.originalname)
    }
})
var save2=multer({storage:myS2});
//上传用户头像
app.post('/headImg',save2.single('file'),function(req,res){
    console.log(req.body,req.file)
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'连接失败'
            })
        }else{
            sqlObj.query(`update account set user_img='${req.body.user_img}' where id=${req.body.Id}`,function(err1,data1){
                if(err1){
                    res.json({
                        code:0,
                        msg:'上传失败'
                    })
                }else{
                    res.json({
                        code:1,
                        msg:'上传成功'
                    })
                }
                sqlObj.end()
            })
        }
    })
})
//用户新增地址
app.post("/addAddress",function(req,res){
    console.log(req.body)
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'连接失败'
            })
        }else{
            if(Math.floor(req.body.isTrue_mo)){
               sqlObj.query(`update address set isTrue_mo=0 where user_Id=${req.body.user_Id}`);
               sqlObj.query(`insert into address (user_Id,address,username,phone,isTrue_mo) 
                values ('${req.body.user_Id}','${req.body.address}','${req.body.username}',
                '${req.body.phone}','${req.body.isTrue_mo}')`,function(err1,data1){
                    if(err1){
                        res.json({
                            code:0,
                            msg:'保存失败'
                        })
                    }else{
                        sqlObj=mysql.createConnection(option)
                        sqlObj.query(`update account set address='${req.body.address}' where Id=${req.body.user_Id}`,function(err2,data2){
                            console.log(err2)
                            if(err2){
                                res.json({
                                    code:0,
                                    msg:'更新默认地址失败'
                                })
                            }else{
                                res.json({
                                    code:1,
                                    msg:'更新默认地址成功'
                                })
                            }
                            
                        })
                    }
                })
            }else{
                  sqlObj.query(`insert into address (user_Id,address,username,phone,isTrue_mo) 
                values ('${req.body.user_Id}','${req.body.address}','${req.body.username}',
                '${req.body.phone}','${req.body.isTrue_mo}')`, function (err1, data1) {
                      if (err1) {
                          res.json({
                              code: 0,
                              msg: '保存失败'
                          })
                      } else {
                         res.json({
                             code:1,
                             msg:'保存地址成功'
                         })
                      }
                  })
            }
            
         
        }
        sqlObj.end()
    })
})
//获取用户地址列表
app.get('/getAddress',function(req,res){
    var sqlObj=mysql.createConnection(option)
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code:0,
                msg:'连接失败'
            })
        }else{
            sqlObj.query(`select * from address where user_Id=${req.query.user_Id}`,function(err1,data1){
                if(err1){
                    res.json({
                        code:0,
                        msg:'获取地址列表失败'
                    })
                }else{
                    res.json({
                        code:1,
                        msg:'获取地址列表成功',
                        data1
                    })
                }
                sqlObj.end()
            })
        }
    })
})
//上传评价照片
var myStorage3=multer.diskStorage({
    destination(req,file,callback){
        callback(null,'./public/imgs/pingJia')
    },
    filename(req,file,callback){
        callback(null,file.originalname)
    }
})
var save3=multer({storage:myStorage3})
//上传评价信息
app.post('/pingImg',save3.single('file'),function(req,res){
    console.log(req.body,req.file) 
    res.json({
        code:1,
        msg:'上传成功'
    })
})
//上传评价
app.post('/pingJia',function(req,res){
    console.log(req.body)
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect(function(err){
        if(err){
            res.json({
                code: 0,
                msg: '连接失败'
            })
        }else{
            var date=new Date();
            sqlObj.query(`insert into pinglun (content,ping_img,ping_time,user_id,food_id,ding_Id,star)
            values ('${req.body.content}','${req.body.ping_img}','${date.toLocaleDateString()} ${date.getHours()}:${date.getMinutes()}',
            '${req.body.user_id}','${req.body.food_id}','${req.body.ding_Id}','${req.body.star}')
            `,function(err1,data1){
                console.log(err1)
                if(err1){
                    res.json({
                        code:0,
                        msg:'评价失败'
                    })
                }else{
                    sqlObj.query(`update dingdan set isTrue_ping=${1} where id=${req.body.ding_Id}`)
                    sqlObj.query(`select * from pei_people where Id=${Math.floor(req.body.pei_Id)}`,function(err2,data2){
                        var sqlObj=mysql.createConnection(option);
                        console.log(data2)
                        sqlObj.query(`update pei_people set goods=${data2[0].goods+Math.floor(req.body.manyi)} where Id=${Math.floor(req.body.pei_Id)}`,function(err3,data3){
                            console.log("fei",data3);
                            
                        })
                    })
                    res.json({
                        code: 1,
                        msg: '评价成功'
                    })
                    
                }
                sqlObj.end()
            })
        }
        
    })
})
//获得所有评价信息
app.get("/getPingJia",function(req,res){
    var sqlObj=mysql.createConnection(option);
    sqlObj.connect((err)=>{
        if(err){
            res.json({
                code:0,
                msg:'连接失败'
            })
        }else{
            sqlObj.query(`select pinglun.*,account.user_img,account.username 
                          from pinglun,account
                          where pinglun.user_id=account.Id
                          and pinglun.Id order by pinglun.ID desc              
            `,function(err1,data1){
                if(err1){
                    res.json({
                        code:0,
                        msg:'获取失败'
                    })
                }else{
                    res.json({
                        code:1,
                        msg:'获取成功',
                        data1,
                    })
                }
                sqlObj.end()
            })
        }
    })
})